﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Cell))]
public class Antibody : MonoBehaviour {

    private Cell m_Cell;
    private float kill_Num=0;
    //爆炸半径
    public float blastRadius;

    
    public GameObject destroyEffect;
    private void Awake(){
      
        m_Cell = GetComponent<Cell>();
    }

    // Update is called once per frame
    void Update () {
       // m_Cell.TargetTag = "Virus";
        GameObject target = m_Cell.UpdateTarget(Blast);
        if (target != null)
        {
            m_Cell.FollowTarget(target);
        }
        else
        {
            m_Cell.Wander();
        }
    }
    public void Blast(GameObject target) {

        // RaycastHit2D[] rh = Physics2D.CircleCastAll(transform.position, blastRadius, transform.up, 0);
        // if (rh != null)
        // {
        //     foreach (RaycastHit2D hit in rh)
        //     {
        //         if (hit.collider.tag == m_Cell.TargetTag)
        //         {

        //             Destroy(hit.collider.gameObject);
        //         }
        //     }

        //     Destroy(this.gameObject);
        // }

        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, blastRadius);
        if (colliders != null) {
            foreach (var item in colliders) {
                if (item.tag == m_Cell.TargetTag) {
                     
                   GameUIController.Instance.gameObject.GetComponent<AudioSource>().Play();
                    if(destroyEffect != null) {
                      GameObject obj = Instantiate(destroyEffect, item.gameObject.transform.position, Quaternion.identity);
                     Destroy(obj, 0.5f);
                  }
                    Destroy(item.gameObject);
                }
            }

            Destroy(this.gameObject);
        }
    }
}
